﻿#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace BeatPong
{
    class MenuSong
    {
        public MenuSong(MenuEntry entry, GameSong s)
        {
            menuEntry = entry;
            song = s;
        }
        public MenuEntry menuEntry;
        public GameSong song;
    }
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class SongPickerScreen : MenuScreen
    {
        #region Fields

        List<MenuSong> songs;
        Dictionary<MenuEntry, GameSong> songtable;
        public string songFolder = "SongXML";

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public SongPickerScreen()
            : base("Songs")
        {
            songs = new List<MenuSong>();
            // Create our menu entries.

            songtable = new Dictionary<MenuEntry, GameSong>();
            
        }

        public override void LoadContent()
        {
            base.LoadContent();

            LoadSongs();
            MenuEntry backMenuEntry = new MenuEntry("Back");

            // Hook up menu event handlers.
            backMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            AddMenuEntries(songs);
            MenuEntries.Add(backMenuEntry);

            
        }

        private void LoadSongs()
        {
            DirectoryInfo di = new DirectoryInfo(songFolder);
            FileInfo[] files = di.GetFiles("*.xml");
            foreach (FileInfo file in files)
            {
                // Load the song without loading the asset via the pipeline
                GameSong song = GameplayScreen.LoadSong(songFolder + "/" + file.Name);
                MenuEntry newentry = new MenuEntry(song.Title + " - " + song.Artist);
                newentry.Selected += SongSelected;
                songtable[newentry] = song;
                songs.Add(new MenuSong(newentry, song));
            }
        }

        private void SongSelected(object sender, PlayerIndexEventArgs e)
        {
            songtable[(MenuEntry)sender].song = ScreenManager.Game.Content.Load<Song>(songtable[(MenuEntry)sender].AssetName);
            GameplayScreen screen = new GameplayScreen();
            screen.song = songtable[(MenuEntry)sender];
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex, screen);
        }
        private void AddMenuEntries(List<MenuSong> songs)
        {
            foreach (MenuSong song in songs)
            {
                MenuEntries.Add(song.menuEntry);
            }
        }

        #endregion

        #region Handle Input

        #endregion
    }
}
